package cn.com.pxto.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import cn.com.pxto.commons.httputil.ParamUtil;
import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.commons.pagesupport.PageUtils;
import cn.com.pxto.model.Lesson;
import cn.com.pxto.model.LessonType;
import cn.com.pxto.service.SchoolManager;

public class LessonAction extends DispatchAction {
	private Log log  = LogFactory.getLog(LessonAction.class);
	private SchoolManager schoolManager=null;
	
	public ActionForward preCreateLesson(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in preCreateLesson method");
		List<LessonType> ltList=schoolManager.listAllLessonType();
		if(ltList == null && ltList.size() == 0){
			return new ActionForward("lessonType.do?method=preCreateLessonType");
		}else{
			request.setAttribute("ltList", ltList);
			return mapping.findForward("preCreateLesson");
		}
	}
	
	public ActionForward doCreateLesson(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in doCreateLesson method");
//		ltid,name
		DynaActionForm lessonForm = (DynaActionForm)form;
		Lesson l = (Lesson)lessonForm.get("lesson");
		boolean s=true;
		if(schoolManager.getLessonByName(l.getName()) == null){
			schoolManager.createLesson(l);
		}else{
			request.setAttribute("message", "课程已存在!");
			s=false;
		}
		request.setAttribute("boolean",s);
		return mapping.findForward("success");
	}
	
	public ActionForward preUpdateLesson(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in preUpdateLesson method");
		List<LessonType> ltList=schoolManager.listAllLessonType();
		int lid=ParamUtil.getIntParams(request, "id");
		Lesson lesson=schoolManager.getLesson(lid);
		request.setAttribute("lesson", lesson);
		request.setAttribute("ltList", ltList);
		return mapping.findForward("preUpdateLesson");
	}
	
	public ActionForward doUpdateLesson(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in doUpdateLesson method");
		DynaActionForm lessonForm = (DynaActionForm)form;
		Lesson l = (Lesson)lessonForm.get("lesson");
		schoolManager.updateLesson(l);
		return listLesson(mapping, form, request, response);
	}
	
	public ActionForward deleteLesson(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in deleteLesson method");
		int lid=ParamUtil.getIntParams(request, "id");
		schoolManager.deleteLesson(lid);
		return listLesson(mapping, form, request, response);
	}
	
	public ActionForward listLesson(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("come in listLesson method");
		PageInfo pageInfo=PageUtils.getPageInfo(request);
		List<Lesson> lessonList=schoolManager.listLesson(pageInfo);
//		List<LessonType> ltList=schoolManager.listAllLessonType();
		request.setAttribute("lessonList", lessonList);
//		request.setAttribute("ltList", ltList);
		request.setAttribute("pageInfo", pageInfo);
		return mapping.findForward("listLesson");
	}

	public void setSchoolManager(SchoolManager schoolManager) {
		this.schoolManager = schoolManager;
	}
	

}
